package com.azdebugit.java8.suanfa.zijie;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SolutionRow {
    
    /**
     * 思路：
     * （1）首先对数组进行排序（从小到大）
     * （2）取出负数部分
     * （3）再取出正数部分
     * ==》数求和问题
     * （4）取0为零界值
     * （5）固定负数部分，取出整数部分，再去找第三个值（从正数部分，和负数部分）
     * （6）统一放到list的integer数组中
     * ==》时间复杂度：O(nlogn)
     * @param
     */
    @Test
    public void streamRandomStriTest() {
        StopWatch sw = new StopWatch();
        sw.start("start");
        int[] num = new int[]{1,2,3,5,6,7,8,-1,-2,-3,-5,-6,-7,-8};
        getRow(num).stream().forEach(x->{
            for (int i = 0; i < x.length; i++) {
                System.out.print(x[i]);
            }
            System.out.print(",");
        });
        sw.stop();
        System.out.println(sw.prettyPrint());

    }
    public List<Integer[]> getRow(int[] num) {
        List<Integer[]> res=new ArrayList<>();
        //若num长度小于3，则返回空 Positive number Negative
        if(num!=null && num.length>3) {
            //从小到大排序
            Arrays.sort(num);
            int[] numNegative = null;
            int[] numPositive =  null;
            int len = num.length;
            int lenNe = 0;
            int lenPos = 0;
            for (int i = 0; i < len; i++) {
                if(num[i]>0){
//                    numFushu = num.
                    System.out.println("-------"+i);
                    numNegative = new int[i];
                    numPositive = new int[len-i];
                    lenNe = i;
                    lenPos = len-i;
                    System.arraycopy(num,0,numNegative,0,i);
                    System.arraycopy(num,i,numPositive,0,len-i);
                    break;
                }
            }
//            能组合多少对n，决定负数的多少m？n<m  -7,-6,-5,

            for (int i = 0; i < lenNe; i++) {
                int i1 = numNegative[i];
                // 1,2,3,4,5,6
//                System.out.println("---"+i1);
                for (int j = 0; j < lenPos; j++) {
                    int j2 = numPositive[j];
                    if(i1+j2==0){
                        continue;
                    }
                    if(i1+j2<0){
                        //再找一个 正数
                        int t1 = i1+j2;
                        int lenTemp = lenPos-j;
                        int[] numPotemp = new int[lenTemp];
                        System.arraycopy(numPositive,j,numPotemp,0,lenTemp);
                        if(needNum(-t1,numPotemp)){
                            res.add(new Integer[]{i1,j2,-t1});
                            continue;
                        }
                    }
                    if(i1+j2>0){
                        //再找一个负数
                        int t2 = i1+j2;
                        int lenTemp = lenNe-i;
                        int[] numNegtemp = new int[lenTemp];
                        System.arraycopy(numNegative,i,numNegtemp,0,lenTemp);
                        if(needNum(-t2,numNegtemp)){
                            res.add(new Integer[]{i1,j2,-t2});
                            continue;
                        }
                    }

                }

            }

        }
        return res;
    }
    public  Boolean needNum(int tag,int[] num){
        Boolean flag = false;
        for (int i = 0; i < num.length; i++) {
            if(tag==num[i]){
                flag = true;
            }
        }
        return flag;
    }
}